/*
 * File Name  : DemoAction.java
 * Author        : wodenwang
 * Date             : 2012-12-11
 */

package com.wodensoft.module;

import static com.wodensoft.core.web.Actions.*;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wodensoft.core.CommonServiceTest;
import com.wodensoft.core.db.CommonService;
import com.wodensoft.core.db.DataPackage;
import com.wodensoft.core.db.DbException;
import com.wodensoft.core.db.POHelper;
import com.wodensoft.core.db.SessionFactoryHelper;
import com.wodensoft.core.web.WebException;

public class DemoAction {

    public DemoAction() {
        // 模拟动态注册（表不存在时建表）Demo对象
        SessionFactoryHelper.Config conf = new SessionFactoryHelper.Config();
        conf.addInputStream(CommonServiceTest.class.getResourceAsStream("/hbm/DEMO.hbm.xml"));
        SessionFactoryHelper.refresh(conf);
    }

    /**
     * demo汇总
     * 
     * @param request
     * @param response
     * @throws WebException
     * @throws DbException
     */
    public void index(HttpServletRequest request, HttpServletResponse response) throws WebException, DbException {
        includePage(request, response, Util.getPagePath(request, "main.jsp"));
    }

    /**
     * 消息展示
     * 
     * @param request
     * @param response
     * @throws WebException
     * @throws DbException
     * @throws InterruptedException
     */
    public void msg(HttpServletRequest request, HttpServletResponse response) throws WebException, DbException,
            InterruptedException {

        TimeUnit.SECONDS.sleep(5);// 等待5秒看loading
        redirectInfoPage(request, response, "ajax测试成功。");
    }

    /**
     * table列表
     * 
     * @param request
     * @param response
     * @throws WebException
     * @throws DbException
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws DbException, WebException {
        // 获取分页信息
        int start = Util.getStart(request);
        int limit = Util.getLimit(request);
        // 获取排序信息
        String field = Util.getSortField(request);
        String dir = Util.getSortDir(request);

        // 随便查询一些什么出来
        // 查询条件
        POHelper.Condition condition = new POHelper.Condition().setNumberBig("id", "22").setOrderByDesc("id");
        condition.setOrderBy(field, dir);

        // 查询Demo实例（DEMO表）
        DataPackage dp = CommonService.getInstance().queryPackage("Demo", start, limit, condition.toEntry());

        // 设置到页面
        request.setAttribute("dp", dp);

        includePage(request, response, Util.getPagePath(request, "list.jsp"));
    }

	/**
	 * 表单
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 */
	public void form(HttpServletRequest request, HttpServletResponse response) throws WebException {
		includePage(request, response, Util.getPagePath(request, "form.jsp"));
	}
	
	/**
	 * 布局
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 */
	public void layout(HttpServletRequest request, HttpServletResponse response) throws WebException {
		includePage(request, response, Util.getPagePath(request, "layout.jsp"));
	}
}
